package com.example.yiparkapplication.activity.index.activity.StaffQuery;


import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.yiparkapplication.R;
import com.example.yiparkapplication.activity.index.activity.discountCard.DiscountCardNoteStreetNameAdapter;
import com.example.yiparkapplication.constant.API;
import com.example.yiparkapplication.util.BaseActivity;
import com.example.yiparkapplication.util.Common;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//AppCompatActivity
public class StaffQueryActivity extends BaseActivity implements View.OnClickListener {
    private JSONObject object;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstancedState){
        super.onCreate(savedInstancedState);
        setContentView(R.layout.activity_staff_query_layout);
        if (Build.VERSION.SDK_INT > 9){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        init();
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.id_staff_query_button:
                EditText accountEditText = findViewById(R.id.id_staff_edit);
                String account = accountEditText.getText().toString();
                if ("".equals(account)) {
                    Toast.makeText(getApplicationContext(), "请输入陆巡工号", Toast.LENGTH_LONG).show();
                    return;
                }
                getKeeperDetail(account);
                break;
        }
    }

    private void init(){
        Button search = findViewById(R.id.id_staff_query_button);
        search.setOnClickListener(this);
    }

    /***
     *
     * @param account
     */
    private void getKeeperDetail(String account){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(API.HOST + API.URL_GET_KEEPER_DETAIL + "?account=" + account)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e){
                Common.failureMessage(e.getMessage(), getApplicationContext());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException{
                String tempResponse = response.body().string();
                JSONObject joOut = Common.tempResponseException(tempResponse, getApplicationContext());
                int code = joOut.getIntValue("code");
                if (code == 100){
                    object = joOut.getJSONObject("object");
                    handler.post(runnable);
                }else if (code == 23006){
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(), "没有该用户", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }else{
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(), joOut.getString("msg"), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    Runnable runnable = new Runnable(){
        @Override
        public void run(){
            if (object != null){
                LinearLayout staffDetails = findViewById(R.id.id_staff_details);
                staffDetails.setVisibility(View.VISIBLE);
                TextView staffName = findViewById(R.id.id_staff_name);
                TextView staffAccount = findViewById(R.id.id_staff_account);
                TextView staffPhone = findViewById(R.id.id_staff_phone);
                TextView staffIDCard = findViewById(R.id.id_staff_ID_card);
                ImageView staffImg = findViewById(R.id.id_staff_img);
                RecyclerView vRecyclerView = findViewById(R.id.id_staff_management_street);

                String name = object.getString("name");
                String account = object.getString("account");
                String phoneNumber = object.getString("phoneNumber");
                String idCardNo = object.getString("idCardNo");
                String streetNameStr = object.getString("streetNameStr");
                String profileUrl = object.getString("profileUrl");

                String [] streetNameArr = streetNameStr.split(",");
                vRecyclerView.setLayoutManager(new LinearLayoutManager(StaffQueryActivity.this));
                DiscountCardNoteStreetNameAdapter adapter = new DiscountCardNoteStreetNameAdapter(streetNameArr, StaffQueryActivity.this);
                vRecyclerView.setAdapter(adapter);

                staffName.setText(name);
                staffAccount.setText(account);
                staffPhone.setText(phoneNumber);
                staffIDCard.setText(idCardNo);
                if (profileUrl != null && "".equals(profileUrl)){
                    staffImg.setImageBitmap(Common.getHttpBitmap(profileUrl));
                }else{
                    staffImg.setImageBitmap(null);
                }
            }
        }
    };
}
